#include "Connection.h"
#include "mysql.h"
#include "PublicUtils.h"

void finish_with_error(MYSQL *con) {
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
}

int MySQLDriver(MYSQL **conn){
    *conn = mysql_init(NULL);

    if (*conn == NULL) {
        fprintf(stderr, "mysql_init() failed\n");
    }

    char *ip = getIpFromConfig();
    char *user = getUserFromConfig();
    char *passwd = getPasswdFromConfig();
    char *database = getDatabaseFromCOnfig();



    // 连接到数据库
    if (mysql_real_connect(*conn, ip, user, passwd, database, 0, NULL, 0) == NULL) {
        finish_with_error(*conn);
    }

    free(ip);
    free(user);
    free(passwd);
    free(database);
    return 1;
}